#!/usr/bin/python3
# -*- coding: utf8 -*-
# Date   : 2021/07/21
# Author : Kwan
# Email  : allenxian@live.com
# Blog   : https://my.oschina.net/u/4173440

from flask import Blueprint, render_template, redirect, url_for, request, json
from flask_login import login_required, current_user
from project.define import admin_required, app_logger
from .page import Messager


# 设备管理
devices = Blueprint(name='devices', import_name='project', url_prefix='/devices', template_folder='routes/devices/html')
# 页面消息管理器
PageMessager = Messager()


@devices.route('/')
@login_required
@admin_required
def default():
    """
    重定向
    """
    return redirect(url_for('devices.gate'))


@devices.route('/gate', methods=['POST', 'GET'])
@login_required
@admin_required
def gate():
    """
    闸机
    """
    view = "gate"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('device_gate.html', **PageMessager.gate_page_data())

@devices.route('/camera', methods=['POST', 'GET'])
@login_required
@admin_required
def camera():
    """
    识别相机
    """
    view = "camera"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('device_camera.html', **PageMessager.camera_page_data())


@devices.route('/led', methods=['POST', 'GET'])
@login_required
@admin_required
def led():
    """
    LED屏
    """
    view = "led"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('device_led.html', **PageMessager.led_page_data())


@devices.route('/cashier', methods=['POST', 'GET'])
@login_required
@admin_required
def cashier():
    """
    收费处
    """
    view = "cashier"
    if request.method == 'POST':
        try:
            # 检查数据类型
            p_data = request.get_json()
            if not isinstance(p_data, dict):
                p_data = json.loads(p_data)
            p_data['View'] = view
            app_logger.debug(f"用户：{current_user.LoginName } 接口：devices.{view} 数据：{p_data}")
            return PageMessager.post_data_handler(**p_data)
        except Exception as e:
            app_logger.error(f"用户：{current_user.LoginName } 接口：devices.{view} 错误：{e}")
        return PageMessager.error('處理錯誤，請聯係管理員')
    # GET
    return render_template('device_cashier.html', **PageMessager.cashier_page_data())